<template>
  <div>
    <!-- 图片分类区域 -->
    <div id="slider" class="mui-slider">
      <div
        id="sliderSegmentedControl"
        class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted"
      >
        <div class="mui-scroll">
          <span :class="['mui-control-item',item.id === 0 ? ' mui-active': '']"  v-for="item in category" 
            :key="item.id" @tap="getPhotoByCategory(item.id)">
            {{item.title}}
          </span>
        </div>
      </div>
    </div>
    <!-- 图片的列表区域 -->
    <ul class="lazyul">
      <router-link tag="li" v-for="item in photolist" :key="item.id" :to="'/home/photoinfo/' + item.id">
      <!-- 注意: v-lazy要指定图片的地址 -->
        <img v-lazy="item.img_url">
        <div class="imge"></div>
        <div class="info">
          <h1 class="info-title"> {{item.seo_title}} </h1>
          <div class="info-content">{{item.zhaiyao}}</div>
        </div>
      </router-link>
    </ul>
  </div>
</template>

<script>
//导入mui的js文件
import mui from "../../../lib/mui/js/mui.js";
export default {
  data() {
    return {
      category: [],
      photolist: []
    };
  },
  created(){
    this.getPhotoCategory();
    this.getPhotoByCategory(0);
  },
  methods: {
    async getPhotoCategory(){
      const {data} = await this.$http.get("http://api.cms.liulongbin.top/api/getimgcategory");
      if(data.status === 0){
        data.message.unshift({title:"全部",id:0});
        this.category = data.message;
      }
    },
    async getPhotoByCategory(id){
      //根据图片分类的id来获取图片
      const {data} = await this.$http.get("http://api.cms.liulongbin.top/api/getimages/" + id);
      if(data.status === 0){
        return (this.photolist = data.message)
      }
    }
  },
  mounted(){
    //当此钩子函数执行时，我们才可以进行控件或插件的初始化工作
    mui(".mui-scroll-wrapper").scroll({
      deceleration: 0.0005, //filck减速系数，系数越大，滚动速度越慢，滚动距离越小
    });
  }
};
</script>

<style lang="scss" scoped>
  .mui-slider{
    touch-action: pan-x;
  }

  .lazyul{
    margin: 0;
    padding: 10px;
    li{
      background-color: #ccc;
      text-align: center;
      box-shadow: 0 0 7px gray;
      position: relative;
      & + li{
        //这种写法适合用于排除第一个应用给其他所有的li
        margin-top: 10px;
      }
      img{
        vertical-align: middle;
      }
      img[lazy=loading] {
        width: 40px;
        height: 300px;
        margin: auto;
      }
      .imge{
        vertical-align: middle;
        width: 40px;
        height: 300px;
        margin: auto;
      }
    }
  }

  .info{
    position: absolute;
    bottom: 0;
    background-color: rgba(0,0,0,0.5);
    color: #fff;
    max-height: 86px;
    overflow: hidden;
    .info-title{
      font-size: 14px;
    }
    .info-content{
      font-size: 13px;
      text-align: left;
      text-indent: 2em;
    }
  }
</style>
